package com.gastudio.Basic;

import org.apache.mina.core.service.IoAcceptor;
import org.apache.mina.core.session.IdleStatus;
import org.apache.mina.filter.codec.ProtocolCodecFilter;
import org.apache.mina.filter.codec.textline.LineDelimiter;
import org.apache.mina.filter.codec.textline.TextLineCodecFactory;
import org.apache.mina.transport.socket.nio.NioSocketAcceptor;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.charset.Charset;

public class MinaServer {
    //设置端口
    static int PORT=8090;
    private static IoAcceptor acceptor=null;
    public static void main(String[] args) throws  IOException {
        acceptor=new NioSocketAcceptor();
        //设置过滤器,主要设置协议的编码解码
        acceptor.getFilterChain().addLast("codec",new ProtocolCodecFilter(
                new TextLineCodecFactory(
                        Charset.forName("UTF-8"),
                        LineDelimiter.WINDOWS.getValue(),
                        LineDelimiter.WINDOWS.getValue()
                )
        ));
        acceptor.getFilterChain().addFirst("filter",new MyServerFilter());
        //设置缓冲区
        acceptor.getSessionConfig().setReadBufferSize(1024);
        //设置多少时间进入休眠状态
        acceptor.getSessionConfig().setIdleTime(IdleStatus.BOTH_IDLE,10);
        //设置handler，真正的业务方法
        acceptor.setHandler(new MyServerHandler());
        //绑定端口
        acceptor.bind(new InetSocketAddress(PORT));
        System.out.printf("Mina Server 已启动！端口号：%s%n", PORT);
    }
}
